/**************************************************************
*   This class is for a serial of followers data op
*   (stores new, compare to smoke out unfollowed ones, etc)
*
*   This is the DB API you should directly use
*
*   Example steps:
*
*
*   By GuiQing, (c)2010     Specialisation Level : followers incrementally comparison
***************************************************************/

/************************
*TO DO:
*   //tableType to be tableType&
*
*   //LEFT JOIN or Cascade select query
*
*   Transactional Op
*
*   T_followers_exported_record
*
*************************/


#ifndef CFOLLOWERSQUERYINC_H_INCLUDED
#define CFOLLOWERSQUERYINC_H_INCLUDED
#include "GlobalConfig.h"

#include "CQuery.h"
#include "CHttpReqLimited.h"
#include "CUser.h"
#include "CHttpReqBLOB.h"

#define CREATE_TABLE_CURRENT_IDS "CREATE TEMP TABLE T_current_ids (id CHAR(12) PRIMARY KEY)"
#define CREATE_TABLE_FRIEND_IDS "CREATE TEMP TABLE T_friend_ids (id CHAR(12) PRIMARY KEY)"

#ifndef INIT_RECORD_COUNT
    #define INIT_RECORD_COUNT 50
#endif
#ifndef GET_ID_COUNT
    #define GET_ID_COUNT 200
#endif

#ifdef GUI_DEPENDENT
wxDECLARE_EVENT(EVT_UPDATE_TOTAL_GAUGE, wxThreadEvent);
wxDECLARE_EVENT(EVT_PULSE_TOTAL_GAUGE, wxThreadEvent);

#define TOTAL_GAUGE_UPDATE(text, num, reciever , ID) \
    wxThreadEvent* totalGaugeUpdateEvent_##ID = new wxThreadEvent(EVT_UPDATE_TOTAL_GAUGE);\
    totalGaugeUpdateEvent_##ID->SetPayload< std::pair<const wxString, const int> >(std::make_pair(text, num)); \
    wxQueueEvent(reciever, totalGaugeUpdateEvent_##ID);

#define TOTAL_GAUGE_PULSE(reciever)  wxQueueEvent(reciever, new wxThreadEvent(EVT_PULSE_TOTAL_GAUGE));
#endif

class CFollowersQueryInc : public CQuery
{
    private:

        CHttpReqLimited myReq;
        CHttpReqBLOB reqBLOB;

        bool createTempTable();
        tableType& compare(const wxString& recordTableName = "T_followers_record");
        bool intoRecycler(tableType& infoTable);
        unsigned long countNewFollowers(const wxString& tableName = "T_followers_record");
        void hasFollowing(tableType& infoTable);

        enum dataStatus
        {
            STATUS_NOT_USED=0,
            DATA_VALID=1,
            DATA_CORRUPTED=2
        };

    public:

        tableType unfollowed;   // It stores the latest unfollowed , just found at this time
                                            //Historical unfollowed will be directly fetched by CQuery::query()

        bool fetchAndStore(const wxString& apiURL,
                                    const wxString& user_id,
                                    const wxString& tableName,
                                    int countStart=20 ,
                                    long nextCursor = -1 ,
                                    long goalNum = -1);
        bool fetchAndStoreBLOB(const wxString& apiURL,
                                            const wxString& user_id,
                                            const wxString& tableName,
                                            const wxString& colName);
        bool find_unfollowed(const wxString& user_id);
        bool update_new_followed(const wxString& user_id);
        //static void set_uid(const wxString& uid_IN);
        void init(const wxString& user_id);
        bool deleteFollowers(tableType& infoTable, const wxString& tableName = "T_followers_record");
        bool clearTable(const wxString& tableName);
        bool initRecordData(const wxString&user_id);
        bool unfollow(const wxString& targetUserID);

#ifdef GUI_DEPENDENT
        //If GUI_DEPENDENT, there must be a pGaugeEventReciever. 'Cause unlike CQuery, this class is not used for something else
        CFollowersQueryInc(wxEvtHandler* pGaugeEventReciever);
#else
        CFollowersQueryInc();
#endif

        virtual ~CFollowersQueryInc();
};


#endif // CFOLLOWERSQUERYINC_H_INCLUDED
/********    Generic Operation Steps  (incrementally sync)   ************
//->HTTP request
//->initDB(create table T_followers_record/recycler/temp ids)
//->insert into ids
//->Compare
//{
//    SELECT *
//    FROM T_followers
//    WHERE id NOT IN (SELECT id FROM T_current_ids)
//}

//if unfo != 0:
    ->GUI output
//    ->store unfollowed to recycler
//   ->Delete unfollowed from T_followers
//   ->Count followers


//if Count(ids)>Count(followers):
//    ->Find new_num
//   {
//        SELECT COUNT(id)
//        FROM T_current_ids
//        WHERE id NOT IN (SELECT id FROM T_followers)
//    }

    ->HTTP Req(&count=new_num)
    ->Insert new
    {
        INSERT
        INTO T_followers
        VALUES (......)
    }
****************************************************/

